package com.fs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.constant.UserConstants;
import com.fs.dao.UserMapper;
import com.fs.entity.Admins;
import com.fs.entity.Attractions;
import com.fs.entity.User;
import com.fs.service.UserService;
import com.fs.util.ResponseData;
import com.fs.util.ResponseDataUtil;
import com.fs.util.ResultEnums;
import com.fs.vo.LoginAdmins;
import com.fs.vo.UserVo;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @author hua
 * @version 1.0
 * @title UserServiceImpl
 * @description
 * @create 2024/4/18 19:36
 */
@Service("UserService")
@Slf4j
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Autowired
    private PasswordService passwordService;
    @Override
    public List<User> select() {
        return null;
    }

    @Override
    public User selectById(Serializable y) {
        return null;
    }

    @Override
    public User find(Object id) {
        return null;
    }

    @Override
    public User findEntity(User user) {
        return null;
    }

    @Override
    public List<User> selectPage(int pageNum, int pageSize) {
        IPage page = new Page(pageNum,pageSize);
        userMapper.selectPage(page,null);
        return page.getRecords();
    }

    @Override
    public List<User> selectPageByUserVo(UserVo userVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return userMapper.selectPageByUserVo(userVo);
    }

    @Override
    public ResponseData Login(String username, String password) {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        // 【2】登录前置校验
        ResponseData responseData = loginPreCheck(username, password);
        if (responseData.getMeta().getStatus()==400){
            return responseData;
        }
        queryWrapper.eq(User::getUsername,username);
        // 【3】用户验证，验证成功获取用户对象
        User user = userMapper.selectOne(queryWrapper);
        if (Objects.isNull(user)) {
            log.info("登录用户：{} 不存在.", username);
            return ResponseDataUtil.buildError(400,"管理员不存在");
        }
        //【4】.校验用户密码
        responseData = passwordService.validate(user, password);
        if (responseData.getMeta().getStatus()==400){
            return responseData;
        }
        return ResponseDataUtil.buildOk(user);
    }


    @Override
    public int delete(Serializable id) {
        return userMapper.deleteById(id);
    }

    @Override
    public int insert(User y) {
        return userMapper.insert(y);
    }

    @Override
    public int update(User y) {
        return userMapper.updateById(y);
    }

    /**
     * 登录前置校验
     *
     * @param username 用户名
     * @param password 用户密码
     */
    public ResponseData loginPreCheck(String username, String password) {
        // 用户名或密码为空 错误
        if (StrUtil.isEmpty(username) || StrUtil.isEmpty(password)) {
            return ResponseDataUtil.buildError(400,"用户名或密码为空");
        }
        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
            return ResponseDataUtil.buildError(400,"用户名不在指定范围内");
        }
        return ResponseDataUtil.buildOk(ResultEnums.OK);
    }

}
